package com.adms.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.adms.domain.Grid;
import com.adms.domain.GridExample;
import com.adms.domain.GridGridUser;
import com.adms.domain.GridGridUserMapper;
import com.adms.domain.GridMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;

@Transactional
@Service
public class GridService {

	@Autowired
	private GridMapper gridMapper;
	@Autowired
	private GridGridUserMapper gridGridUserMapper;

	public Grid getByGridId(Integer gridId) {
		return gridMapper.selectByPrimaryKey(gridId);
	}

	public Grid getByGridIdWithRelativeUsers(Integer gridId) {
		return gridMapper.selectByPrimaryKeyWithRelativeUsers(gridId);
	}
	
	public PageInfo<Grid> list(Grid grid, String timeField, Date start, Date end, String orderByClause, String key,
			Integer pageNum, Integer pageSize, Integer navigatePages) {
		
		GridExample ex = constructExample(grid, timeField, start, end, orderByClause,key);
		PageHelper.startPage(pageNum, pageSize);
		List<Grid> grids = gridMapper.selectByExample(ex);
		return new PageInfo<>(grids, navigatePages);
	}
	
	private GridExample constructExample(Grid grid, String timeField, Date start, Date end,String orderByClause,String key) {
		GridExample ex = new GridExample();
		if (grid != null) {
//			Criteria or = ex.or();
		}

		if (StringUtil.isNotEmpty(timeField)) {
		}

		if (StringUtil.isNotEmpty(orderByClause)) {
			ex.setOrderByClause(orderByClause);
		} else {
			ex.setOrderByClause("grid.grid_id");
		}
		
		if(StringUtil.isNotEmpty(key)) {
			ex.or().andNameLike("%"+key+"%");
		}

		return ex;
	}


	public boolean create(Grid grid,int[] gridUserIds,Integer defaultGridUserId) {
		gridMapper.insertSelective(grid);
		//关联网格员信息
		if(gridUserIds != null && gridUserIds.length > 0) {
			Integer gridId = grid.getGridId();
			List<GridGridUser> gridGridUsers = new ArrayList<>();
			GridGridUser user;
			for(Integer gridUserId:gridUserIds) {
				user = new GridGridUser();
				user.setGridId(gridId);
				user.setGridUserId(gridUserId);
				user.setIsDefault(defaultGridUserId == null?false:gridUserId.equals(defaultGridUserId)?true:false);
				gridGridUsers.add(user);
			}
			gridGridUserMapper.insertBatch(gridGridUsers);
		}
		return true;
	}

	public boolean update(Grid grid,int[] gridUserIds,Integer defaultGridUserId) {
		//判断是否需要更新
		Integer gridId = grid.getGridId();
		Grid grid2 = new Grid();
		grid2.setGridId(gridId);
		if(!grid.equals(grid2)) {
			gridMapper.updateByPrimaryKeySelective(grid);
		}
		//更新关联网格员信息
		if(gridUserIds == null || gridUserIds.length == 0) {
			gridGridUserMapper.deleteByGridId(gridId);
		}else {
			List<GridGridUser> gridGridUsers = new ArrayList<>();
			GridGridUser user;
			for(Integer gridUserId:gridUserIds) {
				user = new GridGridUser();
				user.setGridUserId(gridUserId);
				user.setIsDefault(defaultGridUserId == null?false:gridUserId.equals(defaultGridUserId)?true:false);
				gridGridUsers.add(user);
			}
			gridGridUserMapper.updateBatch(gridId,gridGridUsers);
		}
		return true;
	}

	public boolean delete(Integer[] gridIds) {
		if(gridIds != null) {
			int length = gridIds.length;
			if(length == 1) {
				gridMapper.deleteByPrimaryKey(gridIds[0]);
			}
			if(length > 1) {
				gridMapper.deleteBatch(gridIds);
			}
		}
		return true;
	}

}
